package gr.aueb.mscis.chord;

import gr.aueb.mscis.configuration.Config;
import gr.aueb.mscis.protocol.NodeIdentifier;
import gr.aueb.mscis.protocol.ProtocolType;
import gr.aueb.mscis.protocol.RemoteMessage;

import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.net.InetAddress;
import java.net.Socket;
import java.net.SocketException;
import java.net.UnknownHostException;

import android.util.Log;

public class RemoteMessageClient {
	private ObjectOutputStream oos;
	private ObjectInputStream ois;
	/*oi times autes fortwnontai apo tin Config class*/
	//private String trackerAddr = "192.168.1.3";
	//private String trackerAddr = "localhost";
	//private int trackerPort = 8081;



	public RemoteMessage register() {
		Socket s = null;
		RemoteMessage response = null;
		try {
			s = new Socket(Config.trackerAddr, Config.trackerPort);
		} catch (UnknownHostException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		} catch (IOException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}
		try {
			/*elegxos gia to an to port pou exei eisax8ei einai egguro, alliws epilegetai to default automata*/
			if(Config.listeningPort > 65535 || Config.listeningPort <1000  ){
				Config.listeningPort = Config.defaultPort;
			}
			
			/*dimiourgia node indentifier pou 8a exei mesa to port pou 8a akouei o kombos.Tin IP tha tin parei o tracker apo to socket*/
			/*to port to exei eisagei o xristis. To listening port einai global metabliti pou brisketai
			 * stin class RemoteMessageServer
			 * */
			NodeIdentifier node = new NodeIdentifier(Config.listeningPort);
			// outgoing stream redirect to socket
			oos = new ObjectOutputStream(s.getOutputStream());
			/*mazi me to register stelnetai kai to port pou 8a akouei o kombos*/
			oos.writeObject(new RemoteMessage(ProtocolType.REGISTER, null, node));

			ois = new ObjectInputStream(s.getInputStream());
			response = (RemoteMessage) ois.readObject();

			s.close();

		} catch (SocketException e) {
			 Log.v("register", e.getMessage());

			//System.out.println("Node " + s.getInetAddress() + ":" + s.getPort()
			//		+ " disconnected");
		} catch (UnknownHostException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (ClassNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return response;
	}
	
	public void findSuccessor(InetAddress addr, int port){
		 Socket s = null;
		 try {
			s = new Socket(addr, port);
		} catch (UnknownHostException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		 NodeIdentifier node = new NodeIdentifier();
		 try {
			oos = new ObjectOutputStream(s.getOutputStream());
			oos.writeObject(new RemoteMessage(ProtocolType.FIND_SUCCESSOR, null, node));
			
		 } catch (SocketException e){
			 Log.v("find_successor", e.getMessage());
		 } catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
	}
}
